package com.application.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Network.Protocol;
import com.application.outdoor.InvoikeServices.CommonInvoike;
import com.application.outdoor.InvoikeServices.LogThaoTacInvoike;
import com.application.outdoor.pojo.LogUtil;


public class od_VaoThaoLuanActivity extends Activity {
	TextView txtMsg;
	Button btnDongY, btnTuChoi;
	public static String nguoiGui="";
	public static int idNhom=-1;
	AsyncTask<String, String, String> taskDongY;
	public static ProgressDialog mProgcess;
    @Override
    public void onCreate(Bundle savedInstanceState) {    
    	 Log.d(Protocol.TAG,"od_VaoThaoLuanActivity onCreate1");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Log.d(Protocol.TAG,"od_VaoThaoLuanActivity onCreate2");
        setContentView(R.layout.od_activity_vao_thao_luan);
       
        ActivityController.arrActivity.add(this);
        txtMsg=(TextView)findViewById(R.id.txtMsg);
        btnDongY=(Button)findViewById(R.id.btnDongY);
        btnTuChoi=(Button)findViewById(R.id.btnTuChoi);
        
//        Intent intent=getIntent();
        String msg=nguoiGui+" tạo 1 thảo luận mới! Bạn có muốn tham gia!";
//        if(intent!=null){
//        	nguoiGui=intent.getStringExtra(Protocol.NGUOI_GUI);
//        	idNhom=Integer.parseInt(intent.getStringExtra(Protocol.ID_NHOM));
//        	msg=nguoiGui+" "+msg;
//        	Log.d(GCMIntentService.TAG, "VaoThaoLuanAc n.gui="+nguoiGui);
//        }
        btnTuChoi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();							
			}
		});
        btnDongY.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				taskDongY.execute();
			}
		});
        
        txtMsg.setText(msg);
        mProgcess=new ProgressDialog(this);
        mProgcess.setMessage("Đang vào thảo luận...");
        mProgcess.setCancelable(false);
        initTask();
    }

    private void initTask(){
    	taskDongY=new AsyncTask<String, String, String>(){

			@Override
			protected String doInBackground(String... params) {
				try{
					new CommonInvoike().vaoNhomThaoLuan(CommonUtilities.nguoiDung.getTenDangNhap(), nguoiGui);
					CommonUtilities.idNhomHienTai=idNhom;
					NetworkController._peer.getAccount().isThanhLapNhom=false;
					Log.d(Protocol.TAG,"od_VaoThaoLuanActivity Đồng ý vào nhóm");
					
					//Ghi log
					new LogThaoTacInvoike().insert(CommonUtilities.nguoiDung.getTenDangNhap(), nguoiGui, CommonUtilities.idCayHienTai, LogUtil.VAO_THAO_LUAN);

				}catch (Exception e) {
					Log.e(Protocol.TAG,"Không thể vào nhóm"+e.getMessage());
				}
				return null;
			}
			@Override
			protected void onPostExecute(String ui) {
				
//				Message msg=new Message();
//				Bundle b=new Bundle();
//				b.putString("KEY", "SHOW");
//				msg.setData(b);
//				new Handler().sendMessage(msg);
				finish();
			}

			@Override
			protected void onPreExecute() {
				mProgcess.show();
			}			    		
    	};
    }
    @Override
    public void onDestroy(){
    	super.onDestroy();    	
    }
}
